﻿using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace Gui.Functies.Takenlijst
{
    public partial class Addtaak : UserControl
    {
        private string naam;
        private string status;
        private int projectID;
        private static string conn = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        MySqlConnection grabConnection = new MySqlConnection(conn);    
    
        public Addtaak()
        {
            InitializeComponent();
        }
        //Constructor.
        public Addtaak(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
        }
        //Slaat de nieuwe task op.
        public void SaveTask()
        {
            MySqlCommand taken = new MySqlCommand("INSERT INTO taken (TaakID, Taak, ProjectID, Status)" + "VALUES ('','" + naam + "','" + projectID + "','" + status + "')", grabConnection);

            try
            {
                grabConnection.Open();
                taken.ExecuteNonQuery();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                grabConnection.Close();
                this.Controls.Clear();
                this.Controls.Add(new Takenlijst(projectID));
            }
        }
        //Knop om de methode te starten en de nieuwe task op te slaan.
        private void btnDone_Click(object sender, EventArgs e)
        {
            naam = textBox1.Text;
            status = comboBox1.SelectedItem.ToString();

            if (naam != "" && status != "")
            {
                SaveTask();
            }
            else
            {
                MessageBox.Show("Niet alle velden zijn ingevuld!");
            }
        }
    }
}
